package com.zjht.gmcc.bean.action;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.LinkedHashMap;
import java.util.Map;

import com.zjht.gmcc.bean.util.MyConstant;
import com.zjht.gmcc.bean.util.MyJDBC;

/**
 * RD相关方法
 * @author dyh 2011-10-12
 */
public class RDUtil implements java.io.Serializable {
	private static final long serialVersionUID = 7031158973653091561L;

	// 交易类型
	public final static String DEALTYPE_INIT = "0";// 期初数据
	public final static String DEALTYPE_FILL = "1";// 补货
	public final static String DEALTYPE_SET = "2";// 发生额

	/**
	 * 获取客户名称
	 * @author dyh 2011-10-14
	 * @param custid
	 * @return
	 */
	public static String getCustName(int custid) {
		String custname = "";
		if (custid > 0) {
			MyJDBC myJDBC = null;
			try {
				myJDBC = new MyJDBC();
				String strSQL = "SELECT custname FROM RDCUST WHERE custid=?";
				PreparedStatement ps = myJDBC.prepareStatement(strSQL);
				ps.setInt(1, custid);
				ResultSet rs = ps.executeQuery();
				if (rs.next()) {
					custname = rs.getString(1);
				}
				rs.close();
				rs = null;
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				MyJDBC.close(myJDBC);
			}
		}
		return custname;
	}

	/**
	 * 获取客户ID
	 * @author dyh 2011-10-14
	 * @param custname
	 * @return
	 */
	public static int getCustID(String custname) {
		int custid = MyConstant.INT_NULL;
		if (custid > 0) {
			MyJDBC myJDBC = null;
			try {
				myJDBC = new MyJDBC();
				String strSQL = "SELECT custid FROM RDCUST WHERE custname=?";
				PreparedStatement ps = myJDBC.prepareStatement(strSQL);
				ps.setString(1, custname);
				ResultSet rs = ps.executeQuery();
				if (rs.next()) {
					custid = rs.getInt(1);
				}
				rs.close();
				rs = null;
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				MyJDBC.close(myJDBC);
			}
		}
		return custid;
	}

	/**
	 * 获取客户ID
	 * @author dyh 2011-10-14
	 * @param custname
	 * @return
	 */
	public static Map<String, String> getCustList() {
		Map<String, String> hCust = new LinkedHashMap<String, String>();
		MyJDBC myJDBC = null;
		try {
			myJDBC = new MyJDBC();
			String strSQL = "SELECT custid,custname FROM RDCUST ORDER BY custname";
			PreparedStatement ps = myJDBC.prepareStatement(strSQL);
			ResultSet rs = ps.executeQuery();
			while (rs.next()) {
				hCust.put(rs.getString(1), rs.getString(2));
			}
			rs.close();
			rs = null;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			MyJDBC.close(myJDBC);
		}
		return hCust;
	}

	/**
	 * 获取交易类型名称
	 * @author dyh 2011-10-14
	 * @param dealtype
	 * @return
	 */
	public static String getDealTypeName(String dealtype) {
		String dealtypename = "";
		if (DEALTYPE_INIT.equals(dealtype)) {
			dealtypename = "期初数据";
		}else if (DEALTYPE_FILL.equals(dealtype)) {
			dealtypename = "补货扣款";
		} else if (DEALTYPE_SET.equals(dealtype)) {
			dealtypename = "客户打款";
		}
		return dealtypename;
	}

}